package linehit;

public class LineHit {
   
    // Trajectory equation as before
    
    private static double trajectory(double y,double deg,double v, double g) {
        
        // Deg -> Rad
        double phi = deg * 0.0174532925;
        
        // A,B,C values
        
        double A = ((-g)/(v * v * Math.pow(Math.cos(phi),2)));
        double B = Math.tan(phi);
        double C = y;
        
        // Two outputs for + and -
        
        double x1 = ((-B + Math.sqrt(B*B - (4*A*C)))/ (2*A));
        double x2 = ((-B - Math.sqrt(B*B - (4*A*C)))/ (2*A));
        
        // In this case one will always be 0 so find the one which is
        // and return the other.
        
        if(x1 == 0.0) return x2;
        else return x1;
    }
    
    // Main. Takes the input velocity and Degrees and prints
    // the range.
    
    public static void main(String[] args) {
        Line target = new Line(20d,25d);
        double v = Double.parseDouble(args[0]);
        double phi = Double.parseDouble(args[1]);
        
        // y is always 0 as in this we have flat ground.
        // g is happy in 9.81 m/s
        // hit is where the missile landed
        
        double hit = trajectory(0.0,phi,v,9.81);
        
        
        if(hit<target.x1) {
            double dist = target.x1 - hit;
            // if the distance is smaller than the first point on the line then the missile fell short
            System.out.println("You missed by: " + dist + " to the left of the target.");
        }
        
        else if(target.checkHit(hit)) {
            System.out.println("Direct Hit!");
            System.out.println("Your missile flew " + hit + "m.");
        }
        
        else {
            double dist = hit - target.x2;
            System.out.println("You missed by: " + (-dist) + " to the right of the target.");
        }
        
    }
}
